﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace OASystem.Common
{
    [HubName("myChatHub")]
    public class MyChatHub : Hub
    {

        /// <summary>
        /// 获取连接ID,你可以写成自己的扩展方法,或设置成属性,自行定义
        /// </summary>
        /// <returns></returns>
        public string GetSignalrID()
        {
            if (Context.Request.GetHttpContext().Request.Cookies["userid"] != null)
            {
                return Context.Request.GetHttpContext().Request.Cookies["userid"].Value;
            }
            return "";
        }


        /// <summary>
        /// 用户的connectionID与用户名对照表
        /// </summary>
        private readonly static Dictionary<string, string> connections = new Dictionary<string, string>();




        public override Task OnConnected()
        {
            string kk = Context.ConnectionId;
            return base.OnConnected();
        }
        public void Send(object message)
        {
            //IHubContext chat = GlobalHost.ConnectionManager.GetHubContext<MyChatHub>();
            //chat.Clients.User("2").add(message);
            //Clients.User("2").add(message);
        }
        public void Hello()
        {
            Clients.All.hello();
        }

        /// <summary>
        /// 用户上线函数
        /// </summary>
        /// <param name="name"></param>
        public void SendLogin(string name)
        {
            if (!connections.ContainsKey(name))
            {
                //这里是将用户id和姓名联系起来
                connections.Add(name, Context.ConnectionId);
            }
            else
            {
                //每次登陆id会发生变化
                connections[name] = Context.ConnectionId;
            }
        }

    }
}